package com.gxs.waqas.localfolders.utils;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.file.CopyOption;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.gxs.waqas.localfolders.pojo.ChecksumEntry;

public class ChecksumLoader 
{
	public static HashMap<String, ChecksumEntry> mapOfFilesByChecksum = new HashMap<String, ChecksumEntry>();
	public static HashMap<String, ChecksumEntry> mapOfFilesByFileName = new HashMap<String, ChecksumEntry>();
	public static HashMap<String, ChecksumEntry> mapOfFilesByFolderAndFileName = new HashMap<String, ChecksumEntry>();
	public static HashMap<String, ChecksumEntry> mapOfFilesByAbsoluatePath = new HashMap<String, ChecksumEntry>();
	public static HashMap<String, List<ChecksumEntry> > mapOfAllEntriesByChecksum = new HashMap<String, List<ChecksumEntry>>();
	public static HashMap<String, List<ChecksumEntry>> mapOfAllEntriesByFolder = new HashMap<String, List<ChecksumEntry>>();
	/**
	 * 
	 * @param args
	 */
	public static void main(String [] args)
	{
		try
		{
			String checksumFile = "S:\\checksums.csv";
			String location = "S:\\Pictures and Videos\\_allOtherFolders\\PicsFromBlackLaptop-ProbablyAllOnFlickr";
			String deletedLocation = "S:\\Pictures and Videos\\_deleted";
			//String location = "S:\\Pictures and Videos\\ORGANIZED_CAMERA_PICTURES";
			loadChecksumsIntoMaps(checksumFile);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param checksumFile
	 * @param location
	 * @throws Exception
	 */
	public static void loadChecksumsIntoMaps(String checksumFile) throws Exception 
	{
		FileInputStream fstream = new FileInputStream(checksumFile);
		 // Get the object of DataInputStream
		DataInputStream in = new DataInputStream(fstream);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String strLine;

		int count = 1;
		//Read File Line By Line
		while ((strLine = br.readLine()) != null)   
		{
			String [] sum = strLine.split(",");
			ChecksumEntry entry = new ChecksumEntry(sum[0],sum[1],sum[2],sum[3],sum[4] );
			String checkSum = entry.getChecksum().trim();
			String fileName = entry.getFileName().trim();
			String folderAndFileName = entry.getFolderAndFileName().trim();
			
			File tempFile = new File(entry.getAbsolutePath());
			if(! tempFile.exists() )
			{
				System.out.println("LOADER WARNING: Skipping file becuase it doesn't exist anymore from checksum map: "  + tempFile.getParentFile().getName()+ "\\" + tempFile.getName());
				continue;
			}
			mapOfFilesByChecksum.put(checkSum, entry);
			mapOfFilesByFileName.put(fileName, entry);
			mapOfFilesByFolderAndFileName.put(folderAndFileName, entry);
			mapOfFilesByAbsoluatePath.put(entry.getAbsolutePath(), entry);
			
			//
			// Add to checksum Map for dup tracking
			List<ChecksumEntry> entryList = mapOfAllEntriesByChecksum.get(checkSum);
			if(entryList == null)
			{
				entryList = new ArrayList<ChecksumEntry>();
				mapOfAllEntriesByChecksum.put(checkSum, entryList);
			}
			entryList.add(entry);
			
			
			//
			// add to map by folder
			File currentFolder = new File(entry.getAbsolutePath()).getParentFile();
			entryList = mapOfAllEntriesByFolder.get(currentFolder.getAbsolutePath());
			
			if(entryList == null)
			{
				entryList = new ArrayList<ChecksumEntry>();
				mapOfAllEntriesByFolder.put(currentFolder.getAbsolutePath(), entryList);
			}
			entryList.add(entry);
		}

		System.out.println("Processed files " + (count++));

	}

}
